package com.jic.veteran.component;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jic.veteran.common.pay.SybConstants;
import com.jic.veteran.common.pay.SybUtil;
import com.jic.veteran.dao.DonatePayDetailDao;
import com.jic.veteran.service.DonationReconService;
import com.jic.veteran.service.ProjectDonationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class DonatePayDetailTask {

    @Autowired
    private DonationReconService donationReconService;
    @Autowired
    private DonatePayDetailDao donatePayDetailDao;
    @Value("${recon.file.path}")
    private String RECON_FILE_PATH;

    @Scheduled(cron = "0 0 1 * * *")
    private void dealPayResultTask(){
        String date = DateUtil.format(DateUtil.offsetDay(DateUtil.date(), -1),"yyyy-MM-dd");
        List<Map<String,String>> donatePayDetailList = donatePayDetailDao.getItemsUnSuccess(date);
        for (Map<String,String> map:donatePayDetailList) {
            try {
                String payQuery = donationReconService.payQuery(map.get("reqsn"), map.get("trxid"));
                log.info("查询交易结果:{},支付信息:{}",payQuery,map.toString());
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        }
    }

    @Scheduled(cron = "0 0 6 * * *")
    private void dealReconResultTask(){
        String url = null;
        try {
            //获取对账文件下载地址
            url = donationReconService.getUrl(SybConstants.SYB_SCUSID, DateUtil.format(DateUtil.offsetDay(DateUtil.date(),-1),"yyyyMMdd"));
            log.info("对账完成成功，文件下载地址:{}",url);
        } catch (Exception e) {
            log.info("对账完成出现异常:{}",e.getMessage());
        }
    }
}
